<?php

namespace Admin\Controller;

class CustomerController extends AdminbaseController {

    protected $userModel;

    function _initialize() {
        parent::_initialize();
        $this->userModel = D("user");
    }

    /**
     *  用户管理，有add添加，edit修改，delete删除
     */
    function index() {

        $count = $this->userModel->where()->count();
        $page = $this->page($count, 20);
        $users = $this->userModel->limit($page->firstRow . ',' . $page->listRows)->select();
        $this->assign("users", $users);
        $this->assign("page", $page->show('Admin'));
        $this->display();
    }

    /**
     *  添加管理员
     */
    function add() {
        $this->display();
    }

    /**
     *  添加用户信息
     */
    function add_post() {
        if (IS_POST) {
            $existQQ['qq'] = I('post.qq');
            $existQQ = $this->userModel->where($existQQ)->find();

            if (!isNullOrEmpty($existQQ)) {
                $this->error('该用户的QQ号已存在');
                exit();
            }

            $existMobile['mobile'] = I('post.mobile');
            $existMobile = $this->userModel->where($existMobile)->find();
            if (!isNullOrEmpty($existMobile)) {
                $this->error('该用户的手机号已存在');
                exit();
            }
            $existToken['token'] = I('post.token');
            $existToken = $this->userModel->where($existToken)->find();

            if (!isNullOrEmpty($existToken)) {
                $this->error('该用户的Token已存在');
                exit();
            }

            $data['mobile'] = I('post.mobile');
            $data['create_time'] = getCurrentDate();
            $data['qq'] = I('post.qq');
            $data['status'] = I('post.status');
            $data['head_image_url'] = I('post.imageUrl');
            $data['token'] = I('post.token');

            $this->userModel->add($data);

            $this->success("添加成功！", U('customer/index'));
        }
    }

    /**
     *  编辑用户基本信息
     */
    function edit() {
        $id = I('get.id');

        $user = $this->userModel->where(array('user_id' => $id))->find();
        $this->assign('user', $user);
        $this->display();
    }

    /**
     *  编辑管理员
     */
    function edit_post() {
        if (IS_POST) {
            $id = I('post.id');

            $existQQ['qq'] = I('post.qq');
            $existQQ = $this->userModel->where($existQQ)->find();

            if (!isNullOrEmpty($existQQ)) {
                $this->error('该用户的QQ号已存在');
                exit();
            }

            $existMobile['mobile'] = I('post.mobile');
            $existMobile = $this->userModel->where($existMobile)->find();
            if (!isNullOrEmpty($existMobile)) {
                $this->error('该用户的手机号已存在');
                exit();
            }
            $existToken['token'] = I('post.token');
            $existToken = $this->userModel->where($existToken)->find();

            if (!isNullOrEmpty($existToken)) {
                $this->error('该用户的Token已存在');
                exit();
            }

            $data['mobile'] = I('post.mobile');
            $data['qq'] = I('post.qq');
            $data['status'] = I('post.status');
            $data['head_image_url'] = I('post.imageUrl');
            $data['token'] = I('post.token');

            $this->userModel->where(array('user_id' => $id))->save($data);

            $this->success("更新成功！", U('customer/index'));
        }
    }

    /**
     *  删除
     */
    function delete() {
        $userId = intval(I('get.id'));
        $this->userModel->delete($userId);
        $this->success("删除成功！", U('customer/index'));
    }

}
